﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Prolog
{   
    using SbsSW.SwiPlCs;

    class ConnectProlog
    {
        List<char> stoplist;
        public ConnectProlog()
        {
            stoplist = new List<char>();
            stoplist.AddRange(new char[] { '(', ')', ',', '@','.' });      
        }

        public string Result(string query)
        {
            query.Trim();
            
            Regex r = new Regex(@"[A-Z_][a-zA-Z_]*");
           
            string result = "";
            try
            {
                PlQuery q = new PlQuery(query);

                //foreach (PlTermV s in q.Solutions)
                //{
                //    //result = ConvertHexToString(s[0].ToString());
                //    Console.WriteLine(s[0].ToString());
                //    result += s[0].ToString();
                //}

                bool HasSolution = false;
                //MatchCollection matches = r.Matches(ConvertHexToString(query));
                MatchCollection matches = r.Matches(query);
                foreach (PlQueryVariables v in q.SolutionVariables)
                {
                    HasSolution = true;
                    foreach (Match match in matches)
                    {
                        result += match.ToString() + " = " + v[match.ToString()].ToString() + ";";
                    }
                    //result = ConvertHexToString(result);
                    result += "\r\n";
                }
                if (matches.Count == 0)
                    return HasSolution ? "true" : "false";

                return result;
            }
            catch (SbsSW.SwiPlCs.Exceptions.PlException ex)
            {
                Console.WriteLine(ex.Message);
                return "Error query: " + ex.Message;
            }
        }

        public void InputFile(string s)
        {
                        
            #region Cách 1
            /*
            FileStream fs = new FileStream(s, FileMode.Open);
            StreamReader w = new StreamReader(fs, Encoding.UTF8);
            bool flag;
           
            
            string text;
            while (!w.EndOfStream)
            {
                try
                {
                    text = w.ReadLine();
                    text = text.Trim();
                    {
                        text = text.Replace("\t", " ");
                        text = text.Replace("\r\n", " ");
                        text = text.Replace("\n", " ");
                    }
                    if (text.Length > 3)
                    {
                        if (text[text.Length - 1] == '.')
                        {
                            text = text.Substring(0, text.Length - 1);
                            query += text;                            

                            flag = PlQuery.PlCall("assertz(" + query + ")");
                            if (flag == true)
                                Console.WriteLine(query + "  ");
                            else
                                Console.WriteLine(query + "\t\t==> Unsuccess!!");
                            query = "";
                        }
                        else
                            query += text;
                    }
                }
                catch (SbsSW.SwiPlCs.Exceptions.PlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            w.Close();
            fs.Close();*/
            #endregion
            
            #region Cách 2
            s = s.Replace("\\", "//");
            s = "consult('" + s + "')";
           
            try
            {
                if (PlQuery.PlCall(s))
                    Console.WriteLine("\t==> Success !!");
                else
                    Console.WriteLine("\t==> Unsuccess");
            }
            catch (SbsSW.SwiPlCs.Exceptions.PlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion
        }

        public void InputTerm(string s)
        {
            string query = "assert(" + s + ")";
            try
            {
                PlQuery q = new PlQuery(query);
                q.NextSolution();
                q.Dispose();
                Console.WriteLine("success");
            }
            catch (SbsSW.SwiPlCs.Exceptions.PlException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
